import { getLoadMapList, getTenantID, requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';
import { ILoadMapTypeAll } from '@/common';
import { useCallback, useMemo, useState } from 'react';
import { RuleManageAPI } from '../common/type';
import { getBaseUrl } from '@/services/urlconst';
import { ManageSearchColumns, ManageTabSwitch } from '../mconfig';
// import { ManageSearchColumns } from '../mconfig';
// import { disposeArea } from '../common';
// let baseUrl = 'http://192.168.153.192:31019';

let baseUrl = getBaseUrl();

const RuleManageServices = (curRes: Record<string, any>) => {
  /**
   * 获取数据字典
   * */
  const loadMdicIds: string[] = [
    `${getTenantID()}${ILoadMapTypeAll.PURCHASE_CATEGORY}`,
    `${getTenantID()}${ILoadMapTypeAll.PURCHASE_METHOD}`,
    // `${getTenantID()}${ILoadMapTypeAll.PURCHASE_SUPPLY}`,
    // `${getTenantID()}${ILoadMapTypeAll.PROCURE_KBGCGFS}`,
    // `${getTenantID()}${ILoadMapTypeAll.PROCURE_NCFS}`,
  ];

  /**
   * 获取数据字典loadMdicMap
   * */
  // const loadMdicMapList = useRequest(() => {
  //   return loadMdicMap(loadMdicIds);
  // });

  const { loadMapInfo: loadMdicMapList, mapLoading } = getLoadMapList(loadMdicIds);

  /**
   * 保存当前的搜索参数
   * */
  const [manageTableParams, setManageTableParams] = useState<Record<string, any>>({});

  /**
   * 管理页获取数据方法
   * */
  const { tableProps, data, loading, run, refresh } = useRequest(
    (reqParams: RuleManageAPI.IAssetTableRequestType) => {
      let reqUrl = `${baseUrl}/purchaseStyleRuleController/dataGrid/${reqParams?.params}`;

      let PostParams = {
        current: 1,
        pageSize: 10,
        dicTypeIds: loadMdicIds?.toString(),
        ...reqParams,
      };

      /***保存一份参数**/
      setManageTableParams(PostParams);

      /***发起请求数据接口**/
      return requestFunComponFiex('POST', reqUrl, PostParams);
    },
    {
      manual: true,
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /**
   * 重新请求的方法
   * */
  const reloadRequest = useCallback((reqParams: RuleManageAPI.IAssetTableRequestType | any) => {
    run({
      ...reqParams,
      ...ManageTabSwitch(curRes),
    });
  }, []);

  /**
   * 页面数据及分页信息
   * */
  const tableValues = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  /**
   * 搜索功能配置columns
   * */
  const searchList = useMemo(() => {
    return {
      loadMdicMapList,
      searchList: ManageSearchColumns(loadMdicMapList),
    };
  }, [loadMdicMapList, manageTableParams]);

  return {
    loading,
    reloadRequest,
    manageTableParams,
    reloadRefresh: refresh,
    ...tableValues, //页面列表值
    ...searchList,
  };
};

export { RuleManageServices };
